home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.hitl.washington.edu
/
ftp.hitl.washington.edu.tar
/
ftp.hitl.washington.edu
/
pub
/
people
/
tsoper
/
CT Explorer
/
Eye.cs
< prev
next >
Wrap
Text File
|
2005-06-09
|
9KB
|
379 lines
using System;
using System.Drawing;
//TAKE THESE OUT
using System.ComponentModel;
using System.Collections;
using System.Windows.Forms;
using CsGL.OpenGL;
//TO DO: come up with better name that VIEWING_SIDE
public enum VIEWING_SIDE
{
FRONT,
BACK,
LEFT,
RIGHT,
TOP,
BOTTOM
}
public class Eye
{
//Attributes
private float[] volume = new float[3]; //volume of the model we're viewing
//TO DO: change to startIndex
private float[] index = new float[3]; //position to place origin of model
private float[] orthoVolume = new float[6]; //the volume bounds relative to the eye
private float[] position = new float[3]; //position of the eye
private float[] target = new float[3]; //where the eye is looking
private float[] forwardVector = new float[3]; //the forward vector of the eye
private float[] rightVector = new float[3]; //the right vector relative to the eye
private float maxZoom = 1.0f; //max zoom is 1 (non-zoom-in-able) to start
private float minZoom = 1.0f; //min zoom is 1 (non-zoom-out-able) to start
private float[] zoomFocus = new float[2]; //focus point of the zoom
private float zoomScale = 1.0f; //zoom scale
private float zoomLeft; //ortho volume values when zoomed;
private float zoomRight;
private float zoomBottom;
private float zoomTop;
private float[] zoomCenter = new float[2]; //center position of zoom view
//Properties
public float[] Volume
{
get
{
return (float[])volume.Clone();
}
set
{
volume = (float[])value.Clone();
}
}
public float[] Index
{
get
{
return (float[])index.Clone();
}
set
{
index = (float[])value.Clone();
}
}
public float[] Position
{
get
{
return (float[])position.Clone();
}
set
{
position = (float[])value.Clone();
}
}
public float[] Target
{
get
{
return (float[])target.Clone();
}
set
{
target = (float[])value.Clone();
}
}
private float[] upVector = new float[3];
public float[] UpVector
{
get
{
return (float[])upVector.Clone();
}
set
{
upVector = (float[])value.Clone();
}
}
public float[] RightVector
{
get
{
return (float[])rightVector.Clone();
}
}
public float Left
{
get
{
if (zoomScale.Equals(1.0f))
return orthoVolume[0];
else
return zoomLeft;
}
}
public float Right
{
get
{
if (zoomScale.Equals(1.0f))
return orthoVolume[1];
else
return zoomRight;
}
}
public float Bottom
{
get
{
if(zoomScale.Equals(1.0f))
return orthoVolume[2];
else
return zoomBottom;
}
}
public float Top
{
get
{
if(zoomScale.Equals(1.0f))
return orthoVolume[3];
else
return zoomTop;
}
}
public float Near
{
get
{
return orthoVolume[4];
}
}
public float Far
{
get
{
return orthoVolume[5];
}
}
public float MaxZoom //get/set the maxZoom
{
get
{
return maxZoom;
}
set
{
if(value > 1) //max zoom must be greater than unity
{
maxZoom = value;
}
}
}
public float MinZoom //get/set the min zoom
{
get
{
return minZoom;
}
set
{
minZoom = value;
}
}
public float ZoomScale //get zoom scale
{
get
{
return zoomScale;
}
}
//TO DO: make this a 2D position component
public float[] ZoomFocus //set the focus of the zoom
{
get
{
return (float[])zoomFocus.Clone();
}
set
{
zoomFocus = (float[])value.Clone();
}
}
private float BufferDistance = 100; //buffer distance from volume
//TAKE THIS OUT
public Label lbl = new Label();
//constructor
public Eye()
{
}
public void SetViewingSide( VIEWING_SIDE viewingSide )
{
switch( viewingSide )
{
case VIEWING_SIDE.LEFT:
position[0] = index[0] - BufferDistance;
position[1] = index[1] + volume[1]/2.0f;
position[2] = index[2] + volume[2]/2.0f;
orthoVolume[0] = -volume[1]/2;
orthoVolume[1] = volume[1]/2;
orthoVolume[2] = -volume[2]/2;
orthoVolume[3] = volume[2]/2;
orthoVolume[4] = 0;
orthoVolume[5] = 2*BufferDistance + volume[0];
upVector[0] = upVector[1] = 0.0f;
upVector[2] = 1.0f;
forwardVector.Initialize();
forwardVector[0] = 1.0f;
target = (float[])position.Clone();
target[0] += 1;
break;
case VIEWING_SIDE.RIGHT:
position[0] = index[0] + volume[0] + BufferDistance;
position[1] = index[1] + volume[1]/2.0f;
position[2] = index[2] + volume[2]/2.0f;
orthoVolume[0] = -volume[1]/2;
orthoVolume[1] = volume[1]/2;
orthoVolume[2] = -volume[2]/2;
orthoVolume[3] = volume[2]/2;
orthoVolume[4] = 0;
orthoVolume[5] = 2*BufferDistance + volume[0];
upVector[0] = upVector[1] = 0.0f;
upVector[2] = 1.0f;
forwardVector.Initialize();
forwardVector[0] = -1;
target = (float[])position.Clone();
target[0] -= 1;
break;
case VIEWING_SIDE.TOP:
position[0] = index[0] + volume[0]/2.0f;
position[1] = index[1] + volume[1]/2.0f;
position[2] = index[2] + volume[2] + BufferDistance;
orthoVolume[0] = -volume[0]/2;
orthoVolume[1] = volume[0]/2;
orthoVolume[2] = -volume[1]/2;
orthoVolume[3] = volume[1]/2;
orthoVolume[4] = 0;
orthoVolume[5] = 2*BufferDistance + volume[2];
upVector[0] = upVector[2] = 0.0f;
upVector[1] = 1.0f;
forwardVector.Initialize();
forwardVector[2] = -1;
target = (float[])position.Clone();
target[2] -= 1;
break;
case VIEWING_SIDE.BOTTOM:
position[0] = index[0] + volume[0]/2.0f;
position[1] = index[1] + volume[1]/2.0f;
position[2] = index[2] - BufferDistance;
orthoVolume[0] = -volume[0]/2;
orthoVolume[1] = volume[0]/2;
orthoVolume[2] = -volume[1]/2;
orthoVolume[3] = volume[1]/2;
orthoVolume[4] = 0;
orthoVolume[5] = 2*BufferDistance + volume[2];
upVector[0] = upVector[2] = 0.0f;
upVector[1] = 1.0f;
forwardVector.Initialize();
forwardVector[2] = 1;
target = (float[])position.Clone();
target[2] += 1;
break;
case VIEWING_SIDE.FRONT:
position[0] = index[0] + volume[0]/2.0f;
position[1] = index[1] - BufferDistance;
position[2] = index[2] + volume[2]/2.0f;
orthoVolume[0] = -volume[0]/2;
orthoVolume[1] = volume[0]/2;
orthoVolume[2] = -volume[2]/2;
orthoVolume[3] = volume[2]/2;
orthoVolume[4] = 0;
orthoVolume[5] = 2*BufferDistance + volume[1];
upVector[0] = upVector[1] = 0.0f;
upVector[2] = 1.0f;
forwardVector.Initialize();
forwardVector[1] = 1;
target = (float[])position.Clone();
target[1] += 1;
break;
default:
break;
}
rightVector[0] = forwardVector[1]*upVector[2] -
forwardVector[2]*upVector[1];
rightVector[1] = forwardVector[2]*upVector[0] -
forwardVector[0]*upVector[2];
rightVector[2] = forwardVector[0]*upVector[1] -
forwardVector[1]*upVector[0];
}
//zooms in on a subregion
public void SetZoom(float zs)
{
//make sure the zoom is in range
zoomScale = zs < maxZoom ? zs:maxZoom;
zoomScale = zs > minZoom ? zs:minZoom;
//find the center of the zoomed view (equal to zoomFocus at maxZoom)
//using parametric equations with the variable 't'
float t = (zoomScale - 1)/zoomScale;
//TO DO: zoom out for when zoom is < 1 and > 0
//because center is assumed to be 0,0, parametric equations are simplified
//from zoomCenter = center(1-t) + zoomFocus*t
if(t < 1 && t > 0)
{
zoomCenter[0] = zoomFocus[0]*t;
zoomCenter[1] = zoomFocus[1]*t;
}
else
zoomCenter[0] = zoomCenter[1] = 0; //when zooming out, always focus on center
zoomLeft = zoomCenter[0] + orthoVolume[0]/zoomScale;
zoomRight = zoomCenter[0] + orthoVolume[1]/zoomScale;
zoomBottom = zoomCenter[1] + orthoVolume[2]/zoomScale;
zoomTop = zoomCenter[1] + orthoVolume[3]/zoomScale;
this.lbl.Text = zoomFocus[0] + "," + zoomFocus[1]+ ","+t;
Application.DoEvents();
}
public void SetView()
{
GL.glEnable(GL.GL_DEPTH_TEST);
GL.glMatrixMode(GL.GL_MODELVIEW);
GL.glLoadIdentity();
GL.glOrtho(this.Left,this.Right,this.Bottom,this.Top,
this.Near,this.Far);
GL.gluLookAt(this.Position[0],this.Position[1],this.Position[2],
this.Target[0],this.Target[1],this.Target[2],
this.UpVector[0],this.UpVector[1],this.UpVector[2]);
}
}